package com.neusoft.neuiotms.common.repository.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.model.QSerialNumber;
import com.neusoft.neuiotms.common.model.SerialNumber;
import com.neusoft.neuiotms.common.repository.CustomizedSerialNumberRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedSerialNumberRepositoryImpl extends AbstractRepository
        implements CustomizedSerialNumberRepository {

    @Override
    public Page<SerialNumber> search(final Pageable pageable, final Searchable searchable) {
        final QSerialNumber qSerialNumber = QSerialNumber.serialNumber;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<SerialNumber> query = this.query().selectFrom(qSerialNumber);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return SerialNumber.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QSerialNumber qSerialNumber = QSerialNumber.serialNumber;

        if (!StringUtils.isEmpty(searchable.getStrValue("moduleName"))) {
            where.and(super.containsStr(qSerialNumber.moduleName, searchable, "moduleName"));
        }
        return where;
    }

}
